#!/bin/bash

#XXX: relevant files to search into: *.gn*,*.gyp*,*.idl,*.js,*.grd,*.json,*.htm*
#when editing js and html files, you have to delete stuff instead of commenting them out beacuse otherwise they are still seen

fname='all.patch'
pushd src
#XXX: DO NOT put any # comments inside the following:
#dups are ok, they will be filtered out.
#./ in front is ok, it will be filtered out
allfiles='
'
#chrome/test/BUILD.gn
#'

#was using clang --version
#clang version 3.5.1 (tags/RELEASE_351/final)
#Target: x86_64-unknown-linux-gnu
#Thread model: posix
#and _mm_unpacklo_epi16  seems to be the one causing Illegal instruction in chromium !
#updating clang to 3.6.0:
#clang --version
#clang version 3.6.0 (tags/RELEASE_360/final)
#Target: x86_64-unknown-linux-gnu
#Thread model: posix
#apparently I was NOT using system clang although after updating system clang, and recompiling(ccache is on) a different function (S32_alpha_D32_filter_DX_SSSE3) failed with Illegal instruction than before.

#chrome/browser/devtools/webrtc_device_provider_resources.gyp

allfiles+="$(echo;cat "../patched_files.lst")"
#remove "./" from the beginning, ignoring any whitespace prefix
allfiles="$(sed -re 's|^[[:space:]]*\.\/||g' <<< "$allfiles")"
#echo "${allfiles}"
#echo "${allfiles[@]}"

#sort everything to avoid duplicates! very important, since we append patches without knowing they repeated earlier - if they do, PKGBUILD patch-applying will fail
params="$(sort -bu -- <<< "$allfiles")"
# | read -r -d '' -a params 
#exit 1

#TODO: ensure each file exists! and fail otherwise
#no quotes to allow globbing
#echo git diff --minimal --no-color --irreversible-delete -- ${params[@]}
#set -x
#FIXME: some files in submodules are not git diff-ed!!! eg. third_party/WebKit/Source/bindings/bindings.gni
#git diff --minimal --no-color --irreversible-delete -- ${params[*]} > "/tmp/${fname}"
#set +x

outpatch="/tmp/${fname}"

cat > "$outpatch" <<EOF
make linking succeed with the following side effects:
- removing pdf support (possibly due to webrtc being disabled in args.gn?)
but possibly adding ppapi will allow pdf to link too(?) so it's not required to remove pdf in fact.
- removed gcd_private - for mdns=false to link

EOF

temp_newlist="/tmp/patched_files.lst"
echo -n "" > "$temp_newlist" #start with empty contents

#echo "${params[@]}" | 

#XXX: paths can't have spaces because of this(they will be treated as two different things!):
#yep this takes a while, because it executes git diff for each file!!!
echo "wait like 17.4sec currently"
time for eachf in ${params[@]}; do
  if ! test -f "$eachf" -o -d "$eachf" ; then
    echo "Inexistent file/dir: '$eachf'"
#    echo "please remove and re-run"
#    exit 2
  else
    #existing.
    if test -d "$eachf"; then
      justfname="."
      justpath="$eachf"
    else
      #not a dir, so file:
      justfname="${eachf##*/}"
      justpath="${eachf%$justfname}"
      if test -z "$justpath"; then
        justpath='.'
      fi
    fi
#    echo "justpath: $justpath"
#    echo "justfname: $justfname"
#    echo
#    echo "$eachf"
    if ! test -d "$justpath"; then
      echo "Impossible, dir doesn't exist: '$justpath'"
      exit 2
#      continue
    fi
    pushd "$justpath" >/dev/null
    if ! test -d "$justfname" -o -f "$justfname"; then
      echo "Impossible, file doesn't exist: '$justfname'"
      exit 3
#      continue
    fi
    #must not use "." as path in diff below:
    if test "$justpath" = "."; then
      justpath=""
    fi
    git diff --patch --unified=2 --no-color --irreversible-delete -- "$justfname" | sed -re "s|^((\+\+\+\|\-\-\-) [ab]\\/).*$|\\1${justpath}${justfname}|g" >> "$outpatch"
    popd >/dev/null
    echo "${justpath}${justfname}" >> "$temp_newlist"
  fi
done
popd
cp "${outpatch}" .
cp "$temp_newlist" .

# search for "deleted file mode" or only first 2 words, to see which file is deleted, and comment it out from the above, after you've done a full ./1_sync

